package com.company.utils;

import java.util.Date;  
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.List;  
import java.util.Map;  
import java.util.Set;  
import java.util.Map.Entry;  
  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
import org.apache.poi.ss.usermodel.Cell;  
import org.apache.poi.ss.usermodel.CellStyle;  
import org.apache.poi.ss.usermodel.Row;  
import org.apache.poi.ss.usermodel.Sheet;  
import org.apache.poi.ss.usermodel.Workbook;  
import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  
public class ExcelCreaterImpl implements ExcelCreater {  
      
    private List<Map<String, Object>> datas;  
      
    private final Map<String,Workbook> wbmap = new HashMap<String, Workbook>(2);  
      
    private String dataFormat = "yyyy-MM-dd";  
      
    private String sheetName;  
      
    private boolean isAutoSizeColumn=false;  
      
    private Map<String,String> mess;  
      
      
    public ExcelCreaterImpl(List<Map<String, Object>> datas){  
        init(datas);  
    }  
      
    /** 
     * 初始化 
     * @param datas 
     */  
    private void init(List<Map<String, Object>> datas){  
        this.datas = datas;  
        //构造2003、2007版excel  
        wbmap.put(ExcelCreater.EXCEL_03, new HSSFWorkbook());  
        wbmap.put(ExcelCreater.EXCEL_07, new XSSFWorkbook());  
  
    }  
      
    public void setDateFormat(String dataFormat){  
          
        this.dataFormat = dataFormat;  
    }  
      
    public Workbook createExcel(String excelVerson){  
        Workbook wb = wbmap.get(excelVerson);  
        initExcel(wb);  
        return wb;  
    }  
  
    public Map<String,Workbook> createExcel(){  
  
        Set<Entry<String, Workbook>> wbset = wbmap.entrySet();  
        Iterator<Entry<String, Workbook>> eit = wbset.iterator();  
          
        while(eit.hasNext()){  
            Entry<String, Workbook> workEntry = eit.next();  
            Workbook wb = workEntry.getValue();  
            initExcel(wb);  
        }  
        return wbmap;  
    }  
      
    private void initExcel(Workbook wb){  
        //设置样式  
        CellStyle cellStyle = wb.createCellStyle();//创建样式  
        cellStyle.setDataFormat(wb.createDataFormat().getFormat(dataFormat));//日期格式  
        cellStyle.setAlignment(CellStyle.ALIGN_CENTER);//居中  
          
        //创建一个新的表\并创建名称  
        Sheet s = null==sheetName?s = wb.createSheet():wb.createSheet(sheetName);  
          
          
        for(int i=0; i<datas.size(); i++){ //遍历数据  
              
            Map<String,Object> map = datas.get(i);  
              
            //定义列(列名第一行)  
            Row titleColl = s.createRow(0);  
      
            //定义行(内容第二行起)  
            Row row = s.createRow(i+1);  
              
            Set<Entry<String, Object>> set = map.entrySet();  
              
            Iterator<Entry<String, Object>> it = set.iterator();  
            int x = 0;  
            while(it.hasNext()){  
                //列名  
                Entry<String, Object> entry = it.next();  
                Cell titleCell = titleColl.createCell(x);  
                String titleName = entry.getKey();  
                titleCell.setCellValue(null==mess||null==mess.get(titleName)?titleName:mess.get(titleName));  
                //设置列名样式  
                titleCell.setCellStyle(cellStyle);  
                //内容  
                Cell cell = row.createCell(x);  
                  
                Object value = entry.getValue();  
                  
                if(value instanceof String){  
                    cell.setCellValue(value.toString());  
                }else if(value instanceof  Double){  
                    cell.setCellValue((Double)value);  
                }else if(value instanceof Integer){  
                    cell.setCellValue((Integer)value);  
                }else if(value instanceof Float){  
                    cell.setCellValue((Float)value);  
                }else if(value instanceof Boolean){  
                    cell.setCellValue((Boolean)value);  
                }else if(value instanceof java.util.Date|value instanceof java.sql.Date){  
                    cell.setCellValue((Date)value);  
                }  
                //设置内容样式  
                cell.setCellStyle(cellStyle);  
                  
                  
                x++;  
                  
            }  
            //自动调整列宽  
            if(isAutoSizeColumn) s.autoSizeColumn(i);  
              
        }  
    }  
  
    public List<Map<String, Object>> getDatas() {  
        return datas;  
    }  
  
    public void setDatas(List<Map<String, Object>> datas) {  
        this.datas = datas;  
    }  
  
    public String getSheetName() {  
        return sheetName;  
    }  
  
    public void setSheetName(String sheetName) {  
        this.sheetName = sheetName;  
    }  
  
      
    public Map<String, String> getMess() {  
        return mess;  
    }  
    public void setMess(Map<String, String> mess) {  
        this.mess = mess;  
    }  
    public void isAutoSizeColumn(boolean b) {  
        // TODO Auto-generated method stub  
        this.isAutoSizeColumn = b;  
    }  
  
}  